package com.softer.chengxin.home.dao;

import com.softer.chengxin.home.domin.Message;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2018/9/26
 */
@Mapper
@Repository
public interface MessageDao {
    /**
     * 通过企业id分页查询message
     * @param cropid 企业id
     * @param page 偏移
     * @param limit 调试
     * @return 结果集
     */
    @Select("select @rowno:=@rowno+1 as `row`, m.* from (select @rowno:=#{page}) rn, " +
            "company_message m where cropid=#{cropid} order by m.gmt_create desc limit #{page},#{limit}")
    List<Message> getMessageByCropId(@Param("cropid") String cropid,
                                     @Param("page") int page,
                                     @Param("limit") int limit);

    /**
     * 得到通过企业id分页查询message的条数
     * @param cropid 企业id
     * @return 结果集
     */
    @Select("select count(*) from company_message where cropid=#{cropid}")
    int getMessageCount(@Param("cropid") String cropid);

    /**
     * 新增消息
     * @param messageVO 消息
     * @return int
     */
    @Insert("insert into company_message(title,type,gmt_create,gmt_modified,adder,cropid,url) " +
            "values(#{m.title},#{m.type},now(),now(),#{m.adder},#{m.cropid},#{m.url})")
    int addMessage(@Param("m") Message messageVO);

}
